#!/usr/bin/env bash
tenant="ebf698d1-f101-4031-a88c-4e42c4b13a0b"
page_size="100"
RESULT=`curl http://nacos.op2.clickpaas.tech/nacos/v1/cs/configs?dataId=\&group=\&appName=\&config_tags=\&pageNo=1\&pageSize=$page_size\&tenant=$tenant\&search=accurate`

config_list=`echo "$RESULT" | jq '.pageItems'`
total_count=`echo $RESULT | jq '.totalCount'`
localpath="/home/ec2-user/workspace/nacos/"

echo "write local path is "$localpath

for ((i=0; i<$total_count; i++)); do
    echo "------"
    #创建nacos配置文件，并写入内容
    content=`echo "$config_list" |jq  ".[$i].content"`
    dataId=`echo $config_list |jq -r ".[$i].dataId"`
    echo   -e "$content" | sed -e 's/^"//' -e 's/"$//'>$localpath$dataId
    echo $localpath$dataId" is write"

    #创建nacos接口所需参数文件
    appName=`echo $config_list |jq -r ".[$i].appName"`
    config=`echo $config_list |jq ".[$i]|{id:.id,dataId:.dataId,group:.group,md5:.md5,tenant:.tenant,appName:.appName,type:.type}"`
    md5=`echo $config_list |jq ".[$i].md5"`
    echo -e $config > $localpath$dataId".config"
    echo $localpath$dataId".config is write"
done